package com.qf.openapi.web.master.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 用于处理文件、图片下载请求的controller
 */
@RestController

@Slf4j
public class DownloadController {

    // 处理文件上传路径，使用依赖注入从properties文件中获取配置
    String fileUploadPath = "E:\\JavaProject\\Idea\\File\\file\\";

    @RequestMapping("/downloadUser")
    public void downloadUser(String filename, HttpServletResponse response) throws IOException {
        log.debug("下载导出的文件:{}", filename);
        // 定位要下载的文件的绝对路径
        String finalFilepath = fileUploadPath + filename;
        // 让浏览器以附件的形式下载文件
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        // 把文件内容写入响应对象的输出流中
        IOUtils.copy(new FileInputStream(finalFilepath), response.getOutputStream());
    }

    @RequestMapping("/downloadRout")
    public void downloadRout(String filename, HttpServletResponse response) throws IOException {
        log.debug("下载导出的文件:{}", filename);
        // 定位要下载的文件的绝对路径
        String finalFilepath = fileUploadPath + filename;
        // 让浏览器以附件的形式下载文件
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        // 把文件内容写入响应对象的输出流中
        IOUtils.copy(new FileInputStream(finalFilepath), response.getOutputStream());
    }
}
